﻿@model Core.Models.BusinessUnit

<table border="0" cellspacing="0" cellpadding="5" width="100%">
<tr><td valign="top">
<fieldset>
    <legend>Business Unit Information</legend>
    @Html.HiddenFor(model => model.BusinessUnitId)
    @Html.HiddenFor(model => model.DomainId)
    @Html.Hidden("hdnPluginId")
    @Html.HiddenFor(model => model.PluginIds)

    <div class="editor-label">
        @Html.LabelFor(model => model.BusinessUnitName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.BusinessUnitName, new { size = "30" })
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Phone)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Phone)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Fax)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Fax)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Email, new { size = "30" })
    </div>
</fieldset>
<p>&nbsp;</p>
<fieldset>
    <legend>Authorized Plugins</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.PluginIds)
    </div>
    <div class="editor-field">
        @Html.TextBox("txtPlugin")
        <input type="button" name="PluginAdd" value="Add" onclick="javascript: AddPlugin();" />
        <div id="txtPlugins" class="AutoCompleteResults">@Html.Raw(Model.PluginDisplay)</div>
    </div>
</fieldset>
</td><td valign="top">
<fieldset><legend>Main Address</legend>
    @Html.EditorFor(model => model.Address)

    <div class="editor-label">
        &nbsp;
    </div>
    <div class="editor-field">
        @Html.CheckBoxFor(model => model.IsPublicAddress) @Html.LabelFor(model => model.IsPublicAddress)
    </div>
</fieldset>
</td></tr>
</table>
<script type="text/javascript" language="javascript">
    function RemovePlugin(id) {
        // Remove from the comma separated PluginIds hidden field
        var ids = $("#PluginIds").val();
        var i = ids.toLowerCase().indexOf(id);
        if (i) {
            i -= 1;
            ids = ids.substring(0, i) + ids.substring(i + 37);
            $("#PluginIds").val(ids);
        }
        // Remove from the User Interface
        ids = $("#txtPlugins").html();
        i = ids.toLowerCase().indexOf(id);
        if (i) {
            var t = ids.substring(0, i).toLowerCase();
            var j = t.toLowerCase().lastIndexOf("&nbsp;&lt;<span>");
            ids = ids.substring(0, j) + ids.substring(i + 56);
            $("#txtPlugins").html(ids);
        }
    }

    function AddPlugin() {
        // Add in the BusinessUnit interface div. If the below formatting is changed, it needs to reflect in the BusinessUnitController as well.
        if ($("#hdnPluginId").val() != "") {
            var t;
            if ($("#txtPlugin").val() == "Core")
                t = "&nbsp;&lt;<span>" + $("#txtPlugin").val() + "</span>&gt;";
            else
                t = "&nbsp;&lt;<span>" + $("#txtPlugin").val() + "&nbsp;<a href='javascript: RemovePlugin(\"" + $("#hdnPluginId").val() + "\")'>X</a></span>&gt;";
            var x = $("#txtPlugins").html();
            x += t;
            $("#txtPlugins").html(x);
            // Add the id in the hidden hdnPluginIds field. The id is placed in the hdnPluginId field by the autocomplete select function.
            // Leading comma is used by design for consistency.
            var ids = $("#PluginIds").val();
            ids += "," + $("#hdnPluginId").val()
            $("#PluginIds").val(ids);
            // clear out businessunit input area
            $("#txtPlugin").val("");
            $("#hdnPluginId").val("");
        }
    }

    $(function () {
        $("#txtPlugin")
        .autocomplete({
            select: function (event, ui) {
                $("#hdnPluginId").val(ui.item.id);
            },
            source: function (request, response) {
                $.ajax({
                    url: "/BusinessUnit/Plugins", type: "POST", dataType: "json",
                    data: { search: request.term, maxitems: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.Name, value: item.Name, id: item.Id }
                        }))
                    }
                })
            }
        });
    });
</script>
